package gui;

import sensor.Switcher;
import sensorlogic.SwitcherLogic;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
 * Created by IntelliJ IDEA.
 * User: kate
 * Date: 01.06.2009
 * Time: 1:24:42
 * To change this template use File | Settings | File Templates.
 */
public class SwitcherForm extends JFrame {

    private static SwitcherLogic switcherLogicThread;

    public JPanel stratDevicePanel;
    public JTextField portTextField;
    public JTextField addressTextField;
    public JButton startButton;
    public JLabel portLabel;
    public JLabel addressLabel;
    public JButton stopButton;
    public JPanel switcherPanel;
    public JPanel colorPanel;
    public PictureForm pictureForm;
    //    JLabel imageLabel;
//    Canvas canvas;
//     ImageIcon image = new ImageIcon("kid.jpg");
//    Container cp;


    public Switcher switcher;

    public static void run() {
        JFrame frame = new JFrame("SwitcherForm");
        SwitcherForm form = new SwitcherForm();
        frame.setContentPane(form.switcherPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);

        switcherLogicThread = new SwitcherLogic(form);
        switcherLogicThread.start();
    }

    public SwitcherForm() {

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });


        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ActionEvent: " + e);
                try {
                    stopButton.setEnabled(true);
                    startButton.setEnabled(false);
                    portTextField.setEnabled(false);
                    addressTextField.setEnabled(false);

                    pictureForm = new PictureForm("kidOff.jpg");
                    pictureForm.setVisible(true);
                    System.out.println("Form created!");

                    switcher = new Switcher(portTextField.getText(), Long.parseLong(addressTextField.getText()));
                    System.out.println("Switcher created...");
                    switcher.scan();
                    switcher.connect();
                    System.out.println("Switcher connected...");

                    switcherLogicThread.startProcessing();

                } catch (Exception ex) {
                    stopButton.setEnabled(false);
                    startButton.setEnabled(true);
                    portTextField.setEnabled(true);
                    addressTextField.setEnabled(true);

                    pictureForm.setVisible(false);

                    ConFailed dialog = new ConFailed();
                    dialog.setVisible(true);
                    System.out.println("Switcher connection failed... " + ex);
                }
            }
        });


        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("ActionEvent: " + e);
                stopButton.setEnabled(false);
                /*  autoRadioButton.setEnabled(false);
            manualRadioButton.setEnabled(false);
            timeTextField.setEnabled(false);
            tempTextField.setEnabled(false);
            okButton.setEnabled(false);    */
                startButton.setEnabled(true);
                portTextField.setEnabled(true);
                addressTextField.setEnabled(true);
                // pictureForm.setVisible(false);
                // stopSendButton.setEnabled(false);

                switcherLogicThread.stopProcesing();

                switcher.close();

            }
        });


    }

}
